
Page({
  data: {
    style: '',
    background: ['page1'],
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    circular: false,
    interval: 3000,
    duration: 750,
    previousMargin: 0,
    nextMargin: 0,
    topicList: [],
    options:['','',''] 
  },
  changeProperty: function (e) {
    var propertyName = e.currentTarget.dataset.propertyName
    var newData = {}
    newData[propertyName] = e.detail.value
    this.setData(newData)
  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  changeCircular: function (e) {
    this.setData({
      circular: !this.data.circular
    })
  },
  changeVertical: function (e) {
    this.setData({
      vertical: !this.data.vertical
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },

  // onShow(){
    // wx.hideHomeButton()
  // },

  viewClick: function (e) {
    wx.navigateTo({
      url: '../name/name'
    })
  },
  viewClick2:function(e){
    wx.navigateTo({
      url: '../nickname/nickname'
    })
  },

  //投票点击
  voteClick:function(event){
    var items = this.data.topicList;
    var itemData = event.currentTarget.dataset.value;
    var index = -1;
    for (var i = 0; i < items.length; i++) {
      if(itemData.topicId == items[i].topicId){
        index = i ;
        break;
      }
    }
    if(this.data.topicList[index].isHidden){
      this.data.topicList[index].isHidden = false;
  
      //将用户id，添加到已投票栏目。更新投票的用户数目
      const id = event.currentTarget.dataset.id;
      var objectId = this.data.topicList[index].objectId;
      var count1 = this.data.topicList[index].selectName1Count;
      var count2 = this.data.topicList[index].selectName2Count;
      var count3 = this.data.topicList[index].selectName3Count;

      var Bmob = require('../../dist/Bmob-2.5.2.min');
      const query = Bmob.Query('Topic');
      console.log("id:"+id+"   "+count1);
      query.set('id', objectId) //需要修改的objectId
      if(id==1){
        query.set('selectName1Count',count1+1)
        this.data.topicList[index].selectName1Count = count1+1;
      }else if(id==2){
        query.set('selectName2Count',count2+1)
        this.data.topicList[index].selectName2Count = count2+1;
      }else{
        query.set('selectName3Count',count3+1)
        this.data.topicList[index].selectName3Count = count3+1;
      }
      this.setData({
        ["topicList[" + index + "]"]:   this.data.topicList[index]
       })

      query.save().then(res => {
      console.log(res)
      }).catch(err => {
      console.log(err)
      })
    }
  },
  viewClick3:function(e){
    console.log("click")
    wx.navigateTo({
      url: '../myvote/myvote'
    })
  },
  viewpush:function(e){
    console.log("click")
    wx.navigateTo({
      url: '../votepush/vote'
    })
  },

  topicClick:function(e){
    wx.navigateTo({
      url: '../topic/topic'
    })
  },

  getTopicDataList: function () {
    var Bmob = require('../../dist/Bmob-2.5.2.min');
    const query = Bmob.Query("Topic");
    var now = new Date();
    var year = now.getFullYear(); // 年
    var month = now.getMonth() + 1; // 月
    var day = now.getDate(); // 日
    // 将月份和日期转换为两位数的字符串
    var monthStr = month < 10 ? '0' + month : '' + month;
    var dayStr = day < 10 ? '0' + day : '' + day;
    var currentDayMin=""+year+"-"+monthStr+"-"+dayStr+" 00:00:00"
    var currentDayMax=""+year+"-"+monthStr+"-"+dayStr+" 23:59:59"
    // console.log("currentDayMin:"+currentDayMin)
    // console.log("currentDayMax:"+currentDayMax)
    // query.equalTo("createdAt", ">", currentDayMin);
    // query.equalTo("createdAt", "<", currentDayMax);
    query.limit(3);
    query.order("-createdAt");
    query.find().then(res => {
      //刷新加载数据
      if (Array.isArray(res)) {
        res.forEach(function(item) {
          item.isHidden = true;
        });
        this.setData({
          topicList : res // 将更新后的数组赋值给原数组字段
        });
      }
    });
    console.log("start to begin");
  },
  
  onShow(){
    const app = getApp();
    
    if(app.globalData.hasLoad){
      return;
    }
    app.globalData.hasLoad=true;
  
    //获取topic的信息
    console.log("start to ..."+Date.now());
    this.getTopicDataList();
    console.log("end to ..."+Date.now());
  },
  onReady(){
    console.log("onReady");
    this.setData({style: 'height: 360rpx'})
  },
  onShareAppMessage: function (res) {
    return {
      title: 'AI起名专家',
      path: '/pages/main/main.wxml'
    }
  }
})
